/**
 * 
 */
package cn.edu.nju.ws.sview.wyge.relations.adataset.entities;

import java.util.ArrayList;
import java.util.Iterator;

import com.mysql.jdbc.jdbc2.optional.SuspendableXAConnection;

import cn.edu.nju.ws.sview.cache.URIIDCache;
import cn.edu.nju.ws.sview.cache.URILabelCache;
import cn.edu.nju.ws.sview.rdf.URIResource;
import cn.edu.nju.ws.sview.wyge.util.HTMLEscape;
import cn.edu.nju.ws.sview.wyge.util.Util;

/**
 * @author Weiyi Ge (geweiyi@gmail.com) 2012-7-29
 * 
 */
public class SnippetDisplay {
	public static void main(String[] argus) {
		int ds_id = 4860;
		String ds_type = "uri";
		ArrayList<ISentence> sents = GetNamedEntities.find(ds_type, ds_id,
				136532);
		for (ISentence sent : sents) {
			String resource = sent.getSubject();
			ArrayList<ArrayList<String>> triples = sent.getTriples();
			String res = showSnippet(resource, triples, "en", ds_type, ds_id);
			System.out.println(res);
		}
	}
	/**
	 * 
	 * @param subject
	 * @param triples
	 * @param lang
	 * @param ds_type
	 * @param ds_id
	 * @return <div id="u123">showSnippet()
	 */
	public static String showDivSnippet(String subject,
			ArrayList<ArrayList<String>> triples, String lang, ArrayList<Integer> docs) {

		StringBuffer sb = new StringBuffer();
		try {
			boolean hasRelatedTriples = false;
			for (ArrayList<String> triple : triples) {
				if (triple.get(0).equals(subject)) {
					hasRelatedTriples = true;
					break;
				}
			}
			String entityHrefPrefix = "/sview/entity/o";
			if (subject.startsWith("u")) {
				int subjectURIID = Integer.parseInt(subject.substring(1));
				String subjectURI = URIIDCache.getInstance().getURI(
						subjectURIID);
				String subjectName = URILabelCache.getInstance().getLabel(
						subjectURIID, lang);
				
				if(subjectName.trim().length()==0 && !lang.contentEquals("en")){
					subjectName = URILabelCache.getInstance().getLabel(
							subjectURIID, "en");
				}
				
//				if (subjectName.equals("")) {
				if (subjectName.trim().length()==0) {
					subjectName = Util.getShortenName(subjectURI);
				}
				//=====
				if(subjectName.trim().length() == 0){
					subjectName = URIResource.getLabel(subjectURIID, lang);
				}
				//====
				sb.append("<div id='"+subject+"'>");
				sb.append("<span title='" + subjectURI
						+ "' id='scroll" + subject
						+ "' "//======
						+ " ><a class='subjectUriA' target='_blank' href='"+entityHrefPrefix+subject.substring(1)+"'>"
						+ HTMLEscape.toHtml(subjectName) + "</a></span>");
			} else if (subject.startsWith("b")) {
				String subjectanchor = subject.replace(":", "_");
				sb.append("<div id='"+subjectanchor+"'>");
				sb.append("<span title=\"" + subject
						+ "\" id=\"scroll" + subjectanchor
						+ "\" class='subjectBlanknodeSpan' "//======
						+ ">"
						+ HTMLEscape.toHtml(subject) + "</span>");
			}
			if (hasRelatedTriples) {
				while (true) {
					ArrayList<String> relatedTriple = null;
					Iterator<ArrayList<String>> iter = triples.iterator();
					while (iter.hasNext()) {
						ArrayList<String> triple = iter.next();
						if (triple.get(0).equals(subject)) {
							relatedTriple = triple;
							iter.remove();
							break;
						}
					}
					if (relatedTriple != null) {
						String propertyCode = relatedTriple.get(1);
						int propertyURIID = Integer.parseInt(propertyCode
								.substring(1));
						String propertyURI = URIIDCache.getInstance().getURI(
								propertyURIID);

						String value = relatedTriple.get(2);

						sb.append("<table cellpadding=\"0px\" cellspacing=\"0px\">");
						sb.append("<tr>");

						sb.append("<td style=\"vertical-align:top; padding-left:5px; font-weight:bold;\">&middot;</td>");

						sb.append("<td style=\"vertical-align:top; padding-left:2px;\">");
						String propertyName = URILabelCache.getInstance()
								.getLabel(propertyURIID, lang);
						if (propertyName.equals("")) {
							propertyName = Util.getShortenName(URIIDCache
									.getInstance().getURI(propertyURIID));
						}
						sb.append("<span title='" + propertyURI
								+ "' class='propertySpan' >"
//								+ "' style=\"color:#327800;\" onmouseover=\"this.style.cursor='default';\">"
								+ HTMLEscape.toHtml(propertyName) + "</span>:");
						sb.append("</td>");

						sb.append("<td style=\"vertical-align:top; padding-left:2px;\">");
						if (value.startsWith("u")) {
							int valueURIID = Integer.parseInt(value
									.substring(1));
							String valueURI = URIIDCache.getInstance().getURI(
									valueURIID);
							String valueName = URILabelCache.getInstance()
									.getLabel(valueURIID, lang);
							if (valueName.equals("")) {
								valueName = Util.getShortenName(valueURI);
							}
							int typeid = 0;
							try {
								typeid = URIIDCache
										.getInstance()
										.getURIID(
												"http://www.w3.org/1999/02/22-rdf-syntax-ns#type",
												false);
							} catch (Throwable e1) {
								// TODO Auto-generated catch block
								e1.printStackTrace();
							}
							if (propertyCode.equals("u" + typeid)
									&& !subject.startsWith("b")) {
//								sb.append("<a href=\"javascript:void(0)"
								sb.append("<a target='_blank' href='"+entityHrefPrefix+value.substring(1)//#"+value.substring(1)
										+ "' title='" + valueURI
										+ "' class='objectUriA' "
//										+ "' style=\"color:#0404B4; text-decoration:none;\" onmouseover=\"this.style.textDecoration='underline';\" "
//										+ "onmouseout=\"this.style.textDecoration='none';\" " 
										+ ">"
										+ HTMLEscape.toHtml(valueName) + "</a>");
							} else {
//								int cls = ClassFinder.find(ds_type, ds_id,value);
								int cls = ClassFinder.find(docs,value);
								if (cls == -1) {//if never be subject
									sb.append("<a target='_blank' href='"+entityHrefPrefix+value.substring(1)
											+ "' title='" + valueURI
											+ "' class='objectUriA' >"
//											+ "\" style=\"color:#327800;\" onmouseover=\"this.style.cursor='default';\">"
											+ HTMLEscape.toHtml(valueName)
											+ "</a>");
//									sb.append("<span title=\"" + valueURI
//											+ "\" style=\"color:#327800;\" onmouseover=\"this.style.cursor='default';\">"
//											+ HTMLEscape.toHtml(valueName)
//											+ "</span>");
								} else {
									sb.append("<a target='_blank' href='"+entityHrefPrefix+value.substring(1)//javascript:void(0);"//#scroll"+value
											+ "' title='" + valueURI
											+ "' class='objectUriA' >"
//											+ "\" style=\"color:#0404B4; text-decoration:none;\" onmouseover=\"this.style.textDecoration='underline';\" "
//											+ "onmouseout=\"this.style.textDecoration='none';\" " 
//											+ "class='entityA' "//============
//											+ "onclick=\"requestEntity('" + value + "'," + cls + ")\"" 
											+ HTMLEscape.toHtml(valueName)
											+ "</a>");
								}
							}

						} else if (value.startsWith("b")) {
//							boolean dis = BNodeFinder.find(ds_type, ds_id, value);
							boolean dis = BNodeFinder.find(docs, value);
							String valueanchor = value.replace(":", "_");
							if (dis == false) {
								sb.append("<span title=\""
										+ value
										+ "\" style=\"color:#327800;\" onmouseover=\"this.style.cursor='default';\">"
										+ HTMLEscape.toHtml(value) + "</span>");
							} else {
//								sb.append("<a href=\"javascript:void(0)"
								sb.append("<a target='_blank' href=\"javascript:void(0)"//#"+valueanchor
										+ "\" title=\"" + value
										+ "\" class='objectBlanknodeSpan' "
//										+ "\" style=\"color:#0404B4; text-decoration:none;\" onmouseover=\"this.style.textDecoration='underline';\" "
//										+ "onmouseout=\"this.style.textDecoration='none';\" " 
//										+ " class='blanknodeA' "//==========
//										+ "onclick=\"requestEntity('" + valueanchor + "'," + -1 + ")\"" 
										+ ">"
										+ HTMLEscape.toHtml(value) + "</a>");
							}
							showSnippet(value, triples, lang, docs);
						} else if (value.startsWith("l")) {
							sb.append(value.substring(1));
						}
						sb.append("</td>");

						sb.append("</tr>");
						sb.append("</table>");

						relatedTriple.clear();
					} else {
						break;
					}
				}
			}
		} catch (Throwable t) {
			t.printStackTrace();
		}
		sb.append("<br />");
		sb.append("</div>");
		return sb.toString();
	}
	/**
	 * 
	 * @param subject
	 * @param triples
	 * @param lang
	 * @param ds_type
	 * @param ds_id
	 * @return <div id="u123">showSnippet()
	 */
	public static String showDivSnippet(String subject,
			ArrayList<ArrayList<String>> triples, String lang, String ds_type,
			int ds_id) {

		StringBuffer sb = new StringBuffer();
		try {
			boolean hasRelatedTriples = false;
			for (ArrayList<String> triple : triples) {
				if (triple.get(0).equals(subject)) {
					hasRelatedTriples = true;
					break;
				}
			}
			String entityHrefPrefix = "/sview/entity/o";
			if (subject.startsWith("u")) {
				int subjectURIID = Integer.parseInt(subject.substring(1));
				String subjectURI = URIIDCache.getInstance().getURI(
						subjectURIID);
				String subjectName = URILabelCache.getInstance().getLabel(
						subjectURIID, lang);
				
				if(subjectName.trim().length()==0 && !lang.contentEquals("en")){
					subjectName = URILabelCache.getInstance().getLabel(
							subjectURIID, "en");
				}
				
//				if (subjectName.equals("")) {
				if (subjectName.trim().length()==0) {
					subjectName = Util.getShortenName(subjectURI);
				}
				//=====
				if(subjectName.trim().length() == 0){
					subjectName = URIResource.getLabel(subjectURIID, lang);
				}
				//====
				sb.append("<div id='"+subject+"'>");
				sb.append("<span title='" + subjectURI
						+ "' id='scroll" + subject
						+ "' "//======
						+ " ><a class='subjectUriA' target='_blank' href='"+entityHrefPrefix+subject.substring(1)+"'>"
						+ HTMLEscape.toHtml(subjectName) + "</a></span>");
			} else if (subject.startsWith("b")) {
				String subjectanchor = subject.replace(":", "_");
				sb.append("<div id='"+subjectanchor+"'>");
				sb.append("<span title=\"" + subject
						+ "\" id=\"scroll" + subjectanchor
						+ "\" class='subjectBlanknodeSpan' "//======
						+ ">"
						+ HTMLEscape.toHtml(subject) + "</span>");
			}
			if (hasRelatedTriples) {
				while (true) {
					ArrayList<String> relatedTriple = null;
					Iterator<ArrayList<String>> iter = triples.iterator();
					while (iter.hasNext()) {
						ArrayList<String> triple = iter.next();
						if (triple.get(0).equals(subject)) {
							relatedTriple = triple;
							iter.remove();
							break;
						}
					}
					if (relatedTriple != null) {
						String propertyCode = relatedTriple.get(1);
						int propertyURIID = Integer.parseInt(propertyCode
								.substring(1));
						String propertyURI = URIIDCache.getInstance().getURI(
								propertyURIID);

						String value = relatedTriple.get(2);

						sb.append("<table cellpadding=\"0px\" cellspacing=\"0px\">");
						sb.append("<tr>");

						sb.append("<td style=\"vertical-align:top; padding-left:5px; font-weight:bold;\">&middot;</td>");

						sb.append("<td style=\"vertical-align:top; padding-left:2px;\">");
						String propertyName = URILabelCache.getInstance()
								.getLabel(propertyURIID, lang);
						if (propertyName.equals("")) {
							propertyName = Util.getShortenName(URIIDCache
									.getInstance().getURI(propertyURIID));
						}
						sb.append("<span title='" + propertyURI
								+ "' class='propertySpan' >"
//								+ "' style=\"color:#327800;\" onmouseover=\"this.style.cursor='default';\">"
								+ HTMLEscape.toHtml(propertyName) + "</span>:");
						sb.append("</td>");

						sb.append("<td style=\"vertical-align:top; padding-left:2px;\">");
						if (value.startsWith("u")) {
							int valueURIID = Integer.parseInt(value
									.substring(1));
							String valueURI = URIIDCache.getInstance().getURI(
									valueURIID);
							String valueName = URILabelCache.getInstance()
									.getLabel(valueURIID, lang);
							if (valueName.equals("")) {
								valueName = Util.getShortenName(valueURI);
							}
							int typeid = 0;
							try {
								typeid = URIIDCache
										.getInstance()
										.getURIID(
												"http://www.w3.org/1999/02/22-rdf-syntax-ns#type",
												false);
							} catch (Throwable e1) {
								// TODO Auto-generated catch block
								e1.printStackTrace();
							}
							if (propertyCode.equals("u" + typeid)
									&& !subject.startsWith("b")) {
//								sb.append("<a href=\"javascript:void(0)"
								sb.append("<a target='_blank' href='"+entityHrefPrefix+value.substring(1)//#"+value.substring(1)
										+ "' title='" + valueURI
										+ "' class='objectUriA' "
//										+ "' style=\"color:#0404B4; text-decoration:none;\" onmouseover=\"this.style.textDecoration='underline';\" "
//										+ "onmouseout=\"this.style.textDecoration='none';\" " 
										+ ">"
										+ HTMLEscape.toHtml(valueName) + "</a>");
							} else {
								int cls = ClassFinder.find(ds_type, ds_id,
										value);
								if (cls == -1) {//if never be subject
									sb.append("<a target='_blank' href='"+entityHrefPrefix+value.substring(1)
											+ "' title='" + valueURI
											+ "' class='objectUriA' >"
//											+ "\" style=\"color:#327800;\" onmouseover=\"this.style.cursor='default';\">"
											+ HTMLEscape.toHtml(valueName)
											+ "</a>");
//									sb.append("<span title=\"" + valueURI
//											+ "\" style=\"color:#327800;\" onmouseover=\"this.style.cursor='default';\">"
//											+ HTMLEscape.toHtml(valueName)
//											+ "</span>");
								} else {
									sb.append("<a target='_blank' href='"+entityHrefPrefix+value.substring(1)//javascript:void(0);"//#scroll"+value
											+ "' title='" + valueURI
											+ "' class='objectUriA' >"
//											+ "\" style=\"color:#0404B4; text-decoration:none;\" onmouseover=\"this.style.textDecoration='underline';\" "
//											+ "onmouseout=\"this.style.textDecoration='none';\" " 
//											+ "class='entityA' "//============
//											+ "onclick=\"requestEntity('" + value + "'," + cls + ")\"" 
											+ HTMLEscape.toHtml(valueName)
											+ "</a>");
								}
							}

						} else if (value.startsWith("b")) {
							boolean dis = BNodeFinder.find(ds_type, ds_id,
									value);
							String valueanchor = value.replace(":", "_");
							if (dis == false) {
								sb.append("<span title=\""
										+ value
										+ "\" style=\"color:#327800;\" onmouseover=\"this.style.cursor='default';\">"
										+ HTMLEscape.toHtml(value) + "</span>");
							} else {
//								sb.append("<a href=\"javascript:void(0)"
								sb.append("<a target='_blank' href=\"javascript:void(0)"//#"+valueanchor
										+ "\" title=\"" + value
										+ "\" class='objectBlanknodeSpan' "
//										+ "\" style=\"color:#0404B4; text-decoration:none;\" onmouseover=\"this.style.textDecoration='underline';\" "
//										+ "onmouseout=\"this.style.textDecoration='none';\" " 
//										+ " class='blanknodeA' "//==========
//										+ "onclick=\"requestEntity('" + valueanchor + "'," + -1 + ")\"" 
										+ ">"
										+ HTMLEscape.toHtml(value) + "</a>");
							}
							showSnippet(value, triples, lang, ds_type, ds_id);
						} else if (value.startsWith("l")) {
							sb.append(value.substring(1));
						}
						sb.append("</td>");

						sb.append("</tr>");
						sb.append("</table>");

						relatedTriple.clear();
					} else {
						break;
					}
				}
			}
		} catch (Throwable t) {
			t.printStackTrace();
		}
		sb.append("<br />");
		sb.append("</div>");
		return sb.toString();
	}
	public static String showSnippet(String subject,
			ArrayList<ArrayList<String>> triples, String lang, ArrayList<Integer> docs) {

		StringBuffer sb = new StringBuffer();
		try {
			boolean hasRelatedTriples = false;
			for (ArrayList<String> triple : triples) {
				if (triple.get(0).equals(subject)) {
					hasRelatedTriples = true;
					break;
				}
			}
			if (subject.startsWith("u")) {
				int subjectURIID = Integer.parseInt(subject.substring(1));
				String subjectURI = URIIDCache.getInstance().getURI(
						subjectURIID);
				String subjectName = URILabelCache.getInstance().getLabel(
						subjectURIID, lang);
				
				if(subjectName.trim().length()==0 && !lang.contentEquals("en")){
					subjectName = URILabelCache.getInstance().getLabel(
							subjectURIID, "en");
				}
//				if (subjectName.equals("")) {
				if (subjectName.trim().length()==0) {
					subjectName = Util.getShortenName(subjectURI);
				}
				//=====
				if(subjectName.trim().length() == 0){
					subjectName = URIResource.getLabel(subjectURIID, lang);
				}
				//====
				
				sb.append("<span title=\""
						+ subjectURI
						+ "\" id=\"scroll"
						+ subject
						+ "\" style=\"color:#327800;\" onmouseover=\"this.style.cursor='default';\""
						+ " class='subjectA' "//======
						+ " >"
						+ HTMLEscape.toHtml(subjectName) + "</span>");
			} else if (subject.startsWith("b")) {
				String subjectanchor = subject.replace(":", "_");
				sb.append("<span title=\""
						+ subject
						+ "\" id=\"scroll"
						+ subjectanchor
						+ "\" style=\"color:#327800;\" onmouseover=\"this.style.cursor='default';\" "
						+ " class='subjectA' "//======
						+ ">"
						+ HTMLEscape.toHtml(subject) + "</span>");
			}
			if (hasRelatedTriples) {
				while (true) {
					ArrayList<String> relatedTriple = null;
					Iterator<ArrayList<String>> iter = triples.iterator();
					while (iter.hasNext()) {
						ArrayList<String> triple = iter.next();
						if (triple.get(0).equals(subject)) {
							relatedTriple = triple;
							iter.remove();
							break;
						}
					}
					if (relatedTriple != null) {
						String propertyCode = relatedTriple.get(1);
						int propertyURIID = Integer.parseInt(propertyCode
								.substring(1));
						String propertyURI = URIIDCache.getInstance().getURI(
								propertyURIID);

						String value = relatedTriple.get(2);

						sb.append("<table cellpadding=\"0px\" cellspacing=\"0px\">");
						sb.append("<tr>");

						sb.append("<td style=\"vertical-align:top; padding-left:5px; font-weight:bold;\">&middot;</td>");

						sb.append("<td style=\"vertical-align:top; padding-left:2px;\">");
						String propertyName = URILabelCache.getInstance()
								.getLabel(propertyURIID, lang);
						if (propertyName.equals("")) {
							propertyName = Util.getShortenName(URIIDCache
									.getInstance().getURI(propertyURIID));
						}
						sb.append("<span title=\""
								+ propertyURI
								+ "\" style=\"color:#327800;\" onmouseover=\"this.style.cursor='default';\">"
								+ HTMLEscape.toHtml(propertyName) + "</span>:");
						sb.append("</td>");

						sb.append("<td style=\"vertical-align:top; padding-left:2px;\">");
						if (value.startsWith("u")) {
							int valueURIID = Integer.parseInt(value
									.substring(1));
							String valueURI = URIIDCache.getInstance().getURI(
									valueURIID);
							String valueName = URILabelCache.getInstance()
									.getLabel(valueURIID, lang);
							if (valueName.equals("")) {
								valueName = Util.getShortenName(valueURI);
							}
							int typeid = 0;
							try {
								typeid = URIIDCache
										.getInstance()
										.getURIID(
												"http://www.w3.org/1999/02/22-rdf-syntax-ns#type",
												false);
							} catch (Throwable e1) {
								// TODO Auto-generated catch block
								e1.printStackTrace();
							}
							if (propertyCode.equals("u" + typeid)
									&& !subject.startsWith("b")) {
//								sb.append("<a href=\"javascript:void(0)"
								sb.append("<a href='javascript:void(0);"//#"+value.substring(1)
										+ "' title=\""
										+ valueURI
										+ "\" style=\"color:#0404B4; text-decoration:none;\" onmouseover=\"this.style.textDecoration='underline';\" "
										+ "onmouseout=\"this.style.textDecoration='none';\" " 
										+ "class='classA' "//===========
										+ "onclick='requestClass(" + value.substring(1) + ")'" 
										+ ">"
										+ HTMLEscape.toHtml(valueName) + "</a>");
							} else {
								int cls = ClassFinder.find(docs,value);
								if (cls == -1) {
									sb.append("<span title=\""
											+ valueURI
											+ "\" style=\"color:#327800;\" onmouseover=\"this.style.cursor='default';\">"
											+ HTMLEscape.toHtml(valueName)
											+ "</span>");
								} else {
//									sb.append("<a href=\"javascript:void(0)"
									sb.append("<a href=\"javascript:void(0);"//#scroll"+value
											+ "\" title=\""
											+ valueURI
											+ "\" style=\"color:#0404B4; text-decoration:none;\" onmouseover=\"this.style.textDecoration='underline';\" "
											+ "onmouseout=\"this.style.textDecoration='none';\" " 
											+ "class='entityA' "//============
											+ "onclick=\"requestEntity('" + value + "'," + cls + ")\"" 
											+ ">"
											+ HTMLEscape.toHtml(valueName)
											+ "</a>");
								}
							}

						} else if (value.startsWith("b")) {
							boolean dis = BNodeFinder.find(docs, value);
							String valueanchor = value.replace(":", "_");
							if (dis == false) {
								sb.append("<span title=\""
										+ value
										+ "\" style=\"color:#327800;\" onmouseover=\"this.style.cursor='default';\">"
										+ HTMLEscape.toHtml(value) + "</span>");
							} else {
//								sb.append("<a href=\"javascript:void(0)"
								sb.append("<a href=\"javascript:void(0)"//#"+valueanchor
										+ "\" title=\"" + value
										+ "\" style=\"color:#0404B4; text-decoration:none;\" onmouseover=\"this.style.textDecoration='underline';\" "
										+ "onmouseout=\"this.style.textDecoration='none';\" " 
										+ " class='blanknodeA' "//==========
										+ "onclick=\"requestEntity('" + valueanchor + "'," + -1 + ")\"" 
										+ ">"
										+ HTMLEscape.toHtml(value) + "</a>");
							}
							showSnippet(value, triples, lang, docs);
						} else if (value.startsWith("l")) {
							sb.append(value.substring(1));
						}
						sb.append("</td>");

						sb.append("</tr>");
						sb.append("</table>");

						relatedTriple.clear();
					} else {
						break;
					}
				}
			}
		} catch (Throwable t) {
			t.printStackTrace();
		}
		sb.append("<br />");
		return sb.toString();
	}
	
	public static String showSnippet(String subject,
			ArrayList<ArrayList<String>> triples, String lang, String ds_type,
			int ds_id) {

		StringBuffer sb = new StringBuffer();
		try {
			boolean hasRelatedTriples = false;
			for (ArrayList<String> triple : triples) {
				if (triple.get(0).equals(subject)) {
					hasRelatedTriples = true;
					break;
				}
			}
			if (subject.startsWith("u")) {
				int subjectURIID = Integer.parseInt(subject.substring(1));
				String subjectURI = URIIDCache.getInstance().getURI(
						subjectURIID);
				String subjectName = URILabelCache.getInstance().getLabel(
						subjectURIID, lang);
				
				if(subjectName.trim().length()==0 && !lang.contentEquals("en")){
					subjectName = URILabelCache.getInstance().getLabel(
							subjectURIID, "en");
				}
//				if (subjectName.equals("")) {
				if (subjectName.trim().length()==0) {
					subjectName = Util.getShortenName(subjectURI);
				}
				//=====
				if(subjectName.trim().length() == 0){
					subjectName = URIResource.getLabel(subjectURIID, lang);
				}
				//====
				
				sb.append("<span title=\""
						+ subjectURI
						+ "\" id=\"scroll"
						+ subject
						+ "\" style=\"color:#327800;\" onmouseover=\"this.style.cursor='default';\""
						+ " class='subjectA' "//======
						+ " >"
						+ HTMLEscape.toHtml(subjectName) + "</span>");
			} else if (subject.startsWith("b")) {
				String subjectanchor = subject.replace(":", "_");
				sb.append("<span title=\""
						+ subject
						+ "\" id=\"scroll"
						+ subjectanchor
						+ "\" style=\"color:#327800;\" onmouseover=\"this.style.cursor='default';\" "
						+ " class='subjectA' "//======
						+ ">"
						+ HTMLEscape.toHtml(subject) + "</span>");
			}
			if (hasRelatedTriples) {
				while (true) {
					ArrayList<String> relatedTriple = null;
					Iterator<ArrayList<String>> iter = triples.iterator();
					while (iter.hasNext()) {
						ArrayList<String> triple = iter.next();
						if (triple.get(0).equals(subject)) {
							relatedTriple = triple;
							iter.remove();
							break;
						}
					}
					if (relatedTriple != null) {
						String propertyCode = relatedTriple.get(1);
						int propertyURIID = Integer.parseInt(propertyCode
								.substring(1));
						String propertyURI = URIIDCache.getInstance().getURI(
								propertyURIID);

						String value = relatedTriple.get(2);

						sb.append("<table cellpadding=\"0px\" cellspacing=\"0px\">");
						sb.append("<tr>");

						sb.append("<td style=\"vertical-align:top; padding-left:5px; font-weight:bold;\">&middot;</td>");

						sb.append("<td style=\"vertical-align:top; padding-left:2px;\">");
						String propertyName = URILabelCache.getInstance()
								.getLabel(propertyURIID, lang);
						if (propertyName.equals("")) {
							propertyName = Util.getShortenName(URIIDCache
									.getInstance().getURI(propertyURIID));
						}
						sb.append("<span title=\""
								+ propertyURI
								+ "\" style=\"color:#327800;\" onmouseover=\"this.style.cursor='default';\">"
								+ HTMLEscape.toHtml(propertyName) + "</span>:");
						sb.append("</td>");

						sb.append("<td style=\"vertical-align:top; padding-left:2px;\">");
						if (value.startsWith("u")) {
							int valueURIID = Integer.parseInt(value
									.substring(1));
							String valueURI = URIIDCache.getInstance().getURI(
									valueURIID);
							String valueName = URILabelCache.getInstance()
									.getLabel(valueURIID, lang);
							if (valueName.equals("")) {
								valueName = Util.getShortenName(valueURI);
							}
							int typeid = 0;
							try {
								typeid = URIIDCache
										.getInstance()
										.getURIID(
												"http://www.w3.org/1999/02/22-rdf-syntax-ns#type",
												false);
							} catch (Throwable e1) {
								// TODO Auto-generated catch block
								e1.printStackTrace();
							}
							if (propertyCode.equals("u" + typeid)
									&& !subject.startsWith("b")) {
//								sb.append("<a href=\"javascript:void(0)"
								sb.append("<a href='javascript:void(0);"//#"+value.substring(1)
										+ "' title=\""
										+ valueURI
										+ "\" style=\"color:#0404B4; text-decoration:none;\" onmouseover=\"this.style.textDecoration='underline';\" "
										+ "onmouseout=\"this.style.textDecoration='none';\" " 
										+ "class='classA' "//===========
										+ "onclick='requestClass(" + value.substring(1) + ")'" 
										+ ">"
										+ HTMLEscape.toHtml(valueName) + "</a>");
							} else {
								int cls = ClassFinder.find(ds_type, ds_id,
										value);
								if (cls == -1) {
									sb.append("<span title=\""
											+ valueURI
											+ "\" style=\"color:#327800;\" onmouseover=\"this.style.cursor='default';\">"
											+ HTMLEscape.toHtml(valueName)
											+ "</span>");
								} else {
//									sb.append("<a href=\"javascript:void(0)"
									sb.append("<a href=\"javascript:void(0);"//#scroll"+value
											+ "\" title=\""
											+ valueURI
											+ "\" style=\"color:#0404B4; text-decoration:none;\" onmouseover=\"this.style.textDecoration='underline';\" "
											+ "onmouseout=\"this.style.textDecoration='none';\" " 
											+ "class='entityA' "//============
											+ "onclick=\"requestEntity('" + value + "'," + cls + ")\"" 
											+ ">"
											+ HTMLEscape.toHtml(valueName)
											+ "</a>");
								}
							}

						} else if (value.startsWith("b")) {
							boolean dis = BNodeFinder.find(ds_type, ds_id,
									value);
							String valueanchor = value.replace(":", "_");
							if (dis == false) {
								sb.append("<span title=\""
										+ value
										+ "\" style=\"color:#327800;\" onmouseover=\"this.style.cursor='default';\">"
										+ HTMLEscape.toHtml(value) + "</span>");
							} else {
//								sb.append("<a href=\"javascript:void(0)"
								sb.append("<a href=\"javascript:void(0)"//#"+valueanchor
										+ "\" title=\"" + value
										+ "\" style=\"color:#0404B4; text-decoration:none;\" onmouseover=\"this.style.textDecoration='underline';\" "
										+ "onmouseout=\"this.style.textDecoration='none';\" " 
										+ " class='blanknodeA' "//==========
										+ "onclick=\"requestEntity('" + valueanchor + "'," + -1 + ")\"" 
										+ ">"
										+ HTMLEscape.toHtml(value) + "</a>");
							}
							showSnippet(value, triples, lang, ds_type, ds_id);
						} else if (value.startsWith("l")) {
							sb.append(value.substring(1));
						}
						sb.append("</td>");

						sb.append("</tr>");
						sb.append("</table>");

						relatedTriple.clear();
					} else {
						break;
					}
				}
			}
		} catch (Throwable t) {
			t.printStackTrace();
		}
		sb.append("<br />");
		return sb.toString();
	}
}
